import { makeAutoObservable } from "mobx";

class RouterStore {
  /* 数据 ----------------- */
  // 当前路由keyPath
  currentRouteKeyPath = localStorage.getItem("currentRouteKeyPath")
    ? JSON.parse(localStorage.getItem("currentRouteKeyPath"))
    : [];

  // 面包屑数据
  breadcrumbData = [];

  // 权限菜单列表
  authMenuList = localStorage.getItem("authMenuList")
    ? JSON.parse(localStorage.getItem("authMenuList"))
    : [];

  constructor() {
    makeAutoObservable(this); // 响应式处理

    console.log("从本地获取当前Path", this.currentRouteKeyPath);

    // 初始化面包屑
    // this.currentRouteKeyPath = ["0"];
    this.setBreadcrumbData();
  }

  /* 方法 ----------------- */
  // 清空所有路由数据
  clearAllRouteData = () => {
    // this.authMenuList = [];
    this.currentRouteKeyPath = ["0"];
    this.breadcrumbData = [];
  };

  // 修改权限菜单列表
  setAuthMenuList = (list) => {
    this.authMenuList = list;
  };
  // 修改当前路由keyPath
  setCurrentRouteKeyPath = (keyPath) => {
    keyPath.reverse();
    console.log("setKeyPath", keyPath);

    this.currentRouteKeyPath = keyPath;
    localStorage.setItem("currentRouteKeyPath", JSON.stringify(keyPath));
    this.setBreadcrumbData();
  };

  // 修改面包屑数据
  setBreadcrumbData = () => {
    console.log("当前路径", this.currentRouteKeyPath);
    console.log("nRoutes", this.authMenuList);
    let nkeyPath = this.currentRouteKeyPath;
    let nBreadcrumbData = [];
    let nRoutes = this.authMenuList;

    // 遍历第一层
    nRoutes.forEach((route1) => {
      if (nkeyPath.indexOf(route1.meta.key) != -1) {
        nBreadcrumbData.push({
          path: route1.path,
          title: route1.meta.title,
          isEle: route1.element,
          key: route1.meta.key,
          childrenPath: disposeChildrenPaths(route1),
          childrenKeys: disposeChildrenKeys(route1),
        });

        // 判断是否存在第二层
        if (route1.children) {
          route1.children.forEach((route2) => {
            if (nkeyPath.indexOf(route2.meta.key) != -1) {
              nBreadcrumbData.push({
                path: route2.path,
                title: route2.meta.title,
                isEle: route2.element,
                key: route2.meta.key,
                childrenPath: disposeChildrenPaths(route2),
                childrenKeys: disposeChildrenKeys(route2),
              });

              // 判断是否存在第三层
              if (route2.children) {
                route2.children.forEach((route3) => {
                  if (nkeyPath.indexOf(route3.meta.key) != -1) {
                    nBreadcrumbData.push({
                      path: route3.path,
                      title: route3.meta.title,
                      isEle: route3.element,
                      key: route3.meta.key,
                      childrenPath: disposeChildrenPaths(route3),
                      childrenKeys: disposeChildrenKeys(route3),
                    });
                  }
                });
              }
            }
          });
        }
      }
    });

    console.log("nBreadcrumbData", nBreadcrumbData);

    this.breadcrumbData = nBreadcrumbData;
  };
}

// 子路径数组处理
const disposeChildrenPaths = (route) => {
  let rPaths = [];
  if (route.children) {
    route.children.forEach((cRoute) => {
      rPaths.push(cRoute.path);
    });
  }
  return rPaths;
};

// 子key数组处理
const disposeChildrenKeys = (route) => {
  let rKeys = [];
  if (route.children) {
    route.children.forEach((cRoute) => {
      rKeys.push(cRoute.meta.key);
    });
  }
  return rKeys;
};

// 面包屑路径处理函数
const disposeBreadcrumbPath = (route, level) => {
  console.log("route", route);
  let isEle = route.element;

  console.log(isEle ? "有元素" : "无元素");

  let nPath = "";
  switch (level) {
    case 1:
      nPath = route.path;
      break;

    case 2:
      nPath = route.path;
      // nPath = "/" + route.path.split("/")[3];

      break;

    case 3:
      nPath = route.path;
      break;

    default:
      break;
  }

  console.log(nPath);
};

const routerStore = new RouterStore();
export default routerStore;
